package org.ksug.samples.springmvc;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

@Repository
public class StaticMapAccountRepository implements AccountRepository {

	static Map<String, Account> accounts = new HashMap<String, Account>();

	static {
		try {
			accounts.put("1", new Account("1", "younghoe", Country.KOREA, new SimpleDateFormat("yyMMdd").parse("080621")));
			accounts.put("2", new Account("2", "toby", Country.AUSTRALIA, new SimpleDateFormat("yyMMdd").parse("080621")));
			accounts.put("3", new Account("3", "fromm", Country.KOREA, new SimpleDateFormat("yyMMdd").parse("080809")));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Account findAccount(String number) {
		return accounts.get(number);
	}

	@Override
	public void add(Account account) {
		accounts.put(account.getNumber(), account);
	}

	@Override
	public List<Account> list() {
		return new ArrayList<Account>(accounts.values());
	}

	@Override
	public void delete(String number) {
		accounts.remove(number);
	}

	@Override
	public String getNextNumber() {
		int number = accounts.size();
		while(accounts.containsKey(Integer.toString(number))){
			number++;
		}
		return Integer.toString(number);
	}

}
